<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>基础语法6: v-for列表渲染</title>
    <script src="https://unpkg.com/vue@3"> </script>
</head>
<body>
    <div id="app">
    <h1>列表渲染主要是通过v-for指令实现的</h1>
    <h1>知识点1:迭代普通数组</h1>
    <ul>
        <li v-for="item in flowers">{{item}}</li>        
    </ul>
    <h1>知识点2:迭代对象数组</h1>
        <ul>
            <li v-for="(item ,index) in users">{{index+1}}--{{item.name}}</li>
        </ul>
        <h1>知识点3:迭代数字</h1> 
        <ul>
            <li v-for="item in 10">{{item}}</li>
        </ul>
    <h1>知识点4:关于key属性</h1>
        <h3> 为了提升循环的循环的性能,我们一般会在每个循环加上唯一一个key值,在实际值,后端会向前端返回数据时,携带一个唯一的数据目标条目标准符,一般的一个随机数</h3>
        <div>
            ID:<input type="text" v-model="id"><br>
            NAME: <input type="text" v-model="name"><br>
            <button @click="add()">添加</button>
            <button @click="del()">删除</button><br>
            我新增加的id是{{id}},我新增的name是{{name}}
            <table border="1">
                <tr v-for="(item, index) in users" :key="item.id">
                    <td><input type="checkbox"></td>
                    <td> {{item.id}}</td>
                    <td>{{item.name}}</td>
                </tr>
            </table>
        </div>
     <h1>知识点5:v-for的嵌套</h1>
         <ul>
            <li v-for="(item,index) in users">用户{{index+1}}
                <div>
                    <p v-for="(list,i) in item">{{i}}:{{list}}</p>
                </div>
            </li>
         </ul>


    </div>
</body>
</html>
<script>
    const connApp = Vue.createApp({
        data() {
            return {
              flowers:['西蓝花','蝴蝶花','鸡米花','韭菜花'],
              users:[{id:'001',name:'哈基米',},{id:'002',name:'大狗嚼'},{id:'003',name:'叮咚鸡'},{id:'004',name:'老爷爷'}],
              id:'',
              name:''
            }
        },
        methods: {
            add(){
                this.users.push({id:this.id,name:this.name})//push是从数组尾部进行插入，unshift是从头部进行插入
            },
            del(){
                   this.users.pop()//pop是从数组尾部进行删除，shift是从头部进行删除
            }
        },
    }).mount("#app")
</script>